<template>
  <div class="dropdown" ref="dropdownRef">
    <a class="btn btn-outline-light my-2 dropdown-toggle" href="#" role="button" @click.prevent="openToggle">
      {{ title }}
    </a>
    <div v-show="isOpen" class="dropdown-menu" aria-labelledby="dropdownMenuLink" :style="{ display: 'block' }">
      <slot></slot>
    </div>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref, watch } from 'vue'
import useClickOutside from '../hooks/useClickOutside'

export default defineComponent({
  props: {
    title: {
      type: String,
      require: true
    }
  },
  setup () {
    const isOpen = ref(false)
    const dropdownRef = ref<null | HTMLElement>(null)
    const { isClickOutside } = useClickOutside(dropdownRef)
    const openToggle = () => {
      isOpen.value = !isOpen.value
    }
    watch(isClickOutside, () => {
      if (isClickOutside.value && isOpen.value) {
        isOpen.value = false
      }
    })
    return {
      isOpen,
      openToggle,
      dropdownRef
    }
  }
})
</script>
